﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GpsInfoNS;
using System.IO.Ports;

namespace GpsInfoView
{
    public partial class Form1 : Form
    {
        SerialPort serialPort;
        GpsInfo gpsInfoObj;

        System.Timers.Timer timer;

        public Form1()
        {
            InitializeComponent();

            Control.CheckForIllegalCrossThreadCalls = false;

            serialPort = new SerialPort();
            serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
            cboPortName.Items.AddRange(SerialPort.GetPortNames());

            gpsInfoObj = new GpsInfo();

            timer = new System.Timers.Timer();
            timer.Enabled = false;
            timer.Interval = 50;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);

        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                Gpgga gpgga = gpsInfoObj.GetGpgga();
                lblUtcTime.Text = "UTC Time:" + gpgga._utcHour + ":" + gpgga._utcMinute + ":"  + gpgga._utcSecond;
                lblLat.Text = "Lat:" + gpgga._latitude + gpgga._latitudeDirection;
                lblLon.Text = "Lon:" + gpgga._longitude + gpgga._longitudeDirection;
                lblGpsQI.Text = "Gps Quality:" + gpgga._gpsQualityIndicator;
                lblUsedSatellite.Text = "Used Satellite:" + gpgga._usedSatellites;
                lblSeaLevel.Text = "Sea leavel:" + gpgga._seaLevel + gpgga._seaLevelUnit;

                Gprmc gprmc = gpsInfoObj.GetGprmc();

                lblDate.Text = "Date:" + gprmc._year + gprmc._month + gprmc._day;
                lblSpeed.Text = "Speed(KM/H):" + gprmc._speed;
                lblCog.Text = "Course of ground:" + gprmc._cog;
            }

            catch (Exception)
            {
                
                //do nothing
            }

        }

        void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                byte[] receivedData = new byte[serialPort.BytesToRead];
                serialPort.Read(receivedData, 0, receivedData.Length);
                string strTmp = new ASCIIEncoding().GetString(receivedData);

                rtxtRcv.AppendText(strTmp);

                gpsInfoObj.SaveData(receivedData);
            }
            catch
            {
                //do nothing
            }
        }

        private void btnOpenSerialPort_Click(object sender, EventArgs e)
        {
                if (false == serialPort.IsOpen)
                {
                    try
                    {
                        serialPort.PortName = cboPortName.Text;
                        serialPort.BaudRate = Convert.ToInt32(cboBaudRate.Text);
                        serialPort.DataBits = Convert.ToInt32(cboDataBits.Text);
                        serialPort.StopBits = (StopBits)Convert.ToInt32(cboStopBits.Text);
                        serialPort.Parity = (Parity)Convert.ToInt32(cboParity.SelectedIndex.ToString());

                        serialPort.Open();

                        btnOpenSerialPort.Text = "关闭串口";
                        timer.Enabled = true;
                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show(ex.Message);
                    }

                }
                else
                {
                    try
                    {   
                        serialPort.Close();
                        btnOpenSerialPort.Text = "打开串口";
                        timer.Enabled = false;
                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show(ex.Message);
                    }
                }
        }

        private void btnCleanRcvData_Click(object sender, EventArgs e)
        {
            rtxtRcv.Clear();
        }

        private void rtxtRcv_TextChanged(object sender, EventArgs e)
        {
            rtxtRcv.SelectionStart = rtxtRcv.Text.Length;
            rtxtRcv.ScrollToCaret();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                serialPort.Close();
                btnOpenSerialPort.Text = "打开串口";
                timer.Enabled = false;
                gpsInfoObj.Quit();

            }
            catch (Exception)
            {
                
                //do nothing
            }
        }
    }
}
